package com.guan.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guan.commonutils.R;
import com.guan.eduservice.entity.EduTeacher;
import com.guan.eduservice.entity.vo.TeacherQuery;
import com.guan.eduservice.service.EduTeacherService;
import com.guan.servicebase.exceptonhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-13
 */
@Api(description = "讲师管理") //添加中文描述
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin //解决跨域问题
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    //查询讲师表所有数据 http://localhost:8001/eduservice/teacher/findAll
    @GetMapping("/findAll")
    @ApiOperation("所有讲师列表") //添加中文描述
    public R findAllTeacher(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }


    /** 讲师逻辑删除
     * 1. 数据库中添加逻辑删除标识字段
     * 2. 实体类中逻辑删除的标识属性使用 @TableLogic注解
     * 3. 配置类中添加逻辑删除插件
     *
     * */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("逻辑删除讲师")
    public R removeTeacher(
            @ApiParam(name = "id",value="讲师ID",required = true) //添加中文描述
            @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /** mp 讲师无条件分页查询
     * 1. 配置分页插件
     * 2. 创建Page对象，传入两个参数，当前页和每页最大记录数
     *
     * current 当前页
     * limit 每页记录数
     */
    @GetMapping("/pageTeacher/{current}/{limit}")
    @ApiOperation("无条件分页查询")
    public R pageList(@PathVariable long current,
                      @PathVariable long limit){
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //调用分页方法，在底层会把分页数据封装到pageTeacher对象中
        teacherService.page(pageTeacher, null);

//        try{
//            int a = 10/0;  //除零异常
//        }catch (Exception e){
//            //执行自定义异常
//            throw new GuliException(2001,"执行了自定义异常");
//        }

        long total = pageTeacher.getTotal(); //总记录数
        List<EduTeacher> records = pageTeacher.getRecords();

        //第一种写法
        //return R.ok().data("total",total).data("rows",records);

        //第二种写法
        Map map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);
    }


    /**
     * 讲师-多条件的分页查询
     * @param current
     * @param limit
     * @param teacherQuery 构建条件,加上 @RequestBody 注解，使用json传递数据，把json数据封装到对象里
     *                     但是提交方式需要使用post提交 get得不到数据.
     *                     添加required=false参数后，teacherQuery值可以为空
     * @return
     */
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    @ApiOperation("多条件分页查询")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page对象
        Page<EduTeacher> pageTeahcer = new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，如果不为空，则拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name); //模糊查询方法，字段名，字段值
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.le("gmt_create",end);
        }

        //排序  -- 时间降序
        wrapper.orderByDesc("gmt_create");
        //调用条件查询分页的方法
        teacherService.page(pageTeahcer,wrapper);
        long total = pageTeahcer.getTotal();
        List<EduTeacher> records = pageTeahcer.getRecords();

        return R.ok().data("total",total).data("rows",records);
    }


    /** 添加讲师
     *
     * @param eduTeacher
     * @return
     */
    @PostMapping("/addTeacher")
    @ApiOperation("添加讲师")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }
        return R.error();
    }


    /**
     *  通过 id 查询讲师
     * @param id
     * @return
     */
    @ApiOperation("通过ID查询讲师")
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }


    /**
     *  讲师修改
     *  先查询、在修改
     * @param eduTeacher
     * @return
     */
    @PostMapping("/updateTeacher")
    @ApiOperation("修改讲师信息")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.updateById(eduTeacher);
        if (flag){
            return R.ok();
        }
        return R.error();
    }


}

